<?php

include _ITSTOCK_INCLUDE_DIR_.'html_set_location.inc.php';

itstock_structure($structure, 'hardware');
$edit = false;
$outcase = null;

if(isset($_POST['id'])){
  $structure['where'][0] = sprintf("`%s`.`id`='%s'", $structure['table'], itstock_decode($_POST['id']));
  $edit = itstock_search_all($structure);
}
?><b><?php echo (!$edit?'New ':'')?>Hardware</b>
  <br><br><br>
  <p class="error" id="e_msg" style="display: none;"></p>
<form class="save" action="store.php?type=hardware" method="POST" id="form_hardware" autocomplete="off">
  <input type="hidden" value="<?php echo ($edit?itstock_encode($structure['data'][0]['id']):'')?>" id="id" name="id">
  <table>
    <tr>
      <td><label>Type:</label></td>
      <td><select id="category_id" required autofocus onchange="itstock_request_options('maker_id', 'maker_by_category', 'maker', 'category_id');itstock_location_btn();"><?php
        itstock_structure($categories, 'category');
        if(isset($_POST['ltype'])) {
          switch ($_POST['ltype']) {
            case 'location':
              $categories['where'][0] = 'outcase=1';
              $outcase = '1';
              break;
            case 'cpu':
              $categories['where'][0] = 'outcase=0';
              $outcase = '0';
              break;
          }
        }
        itstock_html_options(null, 'category', 'category_id', $structure, $edit, $categories,'id', 'outcase');
      ?></select>
      </td>
    </tr>
    <tr>
      <td><label>Maker:</label></td>
      <td><select id="maker_id" required onchange="itstock_request_options('model_id', 'model', 'model', 'category_id', 'maker_id');"><?php
        itstock_structure($makers, 'maker_by_category');
        if($edit){
          $makers['where'][0] = 'category_id='.$structure['data'][0]['category_id'];
          itstock_html_options(null, 'maker', 'maker_id',  $structure, $edit, $makers);
        } else {
          echo '<option value="">Please select a Type</option>';
        }
      ?></select>
      </td>
    </tr>
    <tr>
      <td><label>Model:</label></td>
      <td><select name="model_id" id="model_id" required ><?php
        if($edit) {
          itstock_structure($models, 'model');
          $models['where'][0] = 'category_id='.$structure['data'][0]['category_id'];
          $models['where'][1] = 'maker_id='.$structure['data'][0]['maker_id'];
          itstock_html_options(null, 'model', 'model_id',  $structure, $edit, $models);
        } else {
          echo '<option value="">Please select a Maker</option>';
        }
      ?></select>
      </td>
    </tr>
    <tr>
      <td><label>Capacity:</label></td>
      <td><input type="text" name="capacity" style="width: 200px" value="<?php echo ($edit?$structure['data'][0]['capacity']:'')?>" placeholder="Capacity"></td>
    </tr>
    <tr>
      <td><label>Serial Number:</label></td>
      <td><input type="text" name="serialn" style="width: 200px" value="<?php echo ($edit?$structure['data'][0]['serialn']:'')?>" placeholder="Serial #"></td>
    </tr>
    <tr>
      <td><label>Condition:</label></td>
      <td><select name="condition_id" id="condition_id" required ><?php
          itstock_html_options('condition', 'state', 'condition_id', $structure, $edit);
          ?></select>
        </td>
    </tr>
    <tr <?php echo ($edit? '':'style="display: none;"');?> id="location_row">
      <td><label>Choose a Location:</label></td>
      <td>
        <img id="location_btn" onclick="<?php echo $edit?'itstock_locations(\''.($structure['data'][0]['outcase'] === '1'?'locations':'cpus').'\');':''; ?>" src="img/search.png" title="Select Location" />
        <input type="text" data-outcase="<?php echo ($edit?$structure['data'][0]['outcase']:(is_null($outcase)?'':$outcase));?>" value="<?php echo (($edit || !is_null($outcase))?'1':'');?>" style="display:none;" id="location_set" required>
        <img style="float:right;" onclick="itstock_locations('warehouses');" src="img/warehouse.png" title="Move to Warehouse" />
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td id="location_info">
      <?php
        if($edit) {
          if(!is_null($structure['data'][0]['location_id'])) {
            $lid = &$structure['data'][0]['location_id'];
            $ltype = 'location';
          } else if(!is_null($structure['data'][0]['cpu_id'])) {
            $lid = &$structure['data'][0]['cpu_id'];
            $ltype = 'cpu';
          } else if(!is_null($structure['data'][0]['warehouse_id'])) {
            $lid = &$structure['data'][0]['warehouse_id'];
            $ltype = 'warehouse';
          }
          itstock_page_set_location(itstock_encode($lid), $ltype);
        }
        if(isset($_POST['lid']) && isset($_POST['ltype'])) {
          itstock_page_set_location($_POST['lid'], $_POST['ltype']);
        }
      ?>
      </td>
    </tr>
    <tr>
      <td><label onclick="HS('textarea_hardware');" title="Click to show/hide">Comments:</label></td>
      <td><textarea <?php echo ($edit && $structure['data'][0]['comments'] !== ""? '':'style="display: none;"');?> form="form_hardware" id="textarea_hardware" name="comments"><?php echo ($edit?$structure['data'][0]['comments']:'')?></textarea></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><br><input type="submit" value="Save" style="margin-right: 100px;"><input type="reset" value="Reset"></td>
    </tr>
  </table>
</form>
